package com.hzdp.web.action.review.ajax;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hzdp.entity.UserAccount;
import com.hzdp.review.bean.ReviewListRequest;
import com.hzdp.review.bean.ReviewListResponse;
import com.hzdp.review.entity.Review;
import com.hzdp.review.service.ReviewService;
import com.hzdp.user.service.UserAccountService;
import com.hzdp.web.action.AjaxBaseAction;
import com.hzdp.web.action.review.vo.ReviewVo;

public class AjaxReviewListAction extends AjaxBaseAction {

	private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	private int productId;
	private int productType;
	private int reviewType;
	private int skin;
	private int age;
	private int offset;
	private int pageSize;
	private List<ReviewVo> reviews = new ArrayList<ReviewVo>();;

	@Autowired
	private ReviewService reviewService;

	@Autowired
	private UserAccountService userAccountService;

	@Override
	protected boolean checkParams() {
		if (productId <= 0 || productType <= 0 || reviewType <= 0) {
			type = 2;
			return false;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return true;
	}

	@Override
	protected void executeJson() {
		type = 0;
		ReviewListRequest request = new ReviewListRequest();
		request.setAge(age);
		request.setOffset(offset);
		request.setPageSize(pageSize);
		request.setProductId(productId);
		request.setProductType(productType);
		request.setReviewType(reviewType);
		request.setSkin(skin);
		ReviewListResponse response = reviewService.pageReviews(request);
		List<Integer> userIds = new ArrayList<Integer>();
		if (response != null) {
			type = 1;
			List<Review> reviewList = response.getReviewList();
			Map<Integer, ReviewVo> reviewMap = new HashMap<Integer, ReviewVo>();
			for (Review review : reviewList) {
				ReviewVo vo = new ReviewVo();
				int userId = review.getUserId();
				vo.setUserId(userId);
				vo.setReviewAge(review.getAge());
				vo.setReviewContent(review.getReview());
				vo.setReviewSkin(review.getSkin());
				vo.setReviewStar(review.getGrade1());
				vo.setReviewTime(df.format(review.getAddtime()));
				userIds.add(userId);
				reviewMap.put(userId, vo);
				reviews.add(vo);
			}

			List<UserAccount> userAccounts = userAccountService.findByIds(userIds);
			for (UserAccount userAccount : userAccounts) {
				int userId = userAccount.getId();
				ReviewVo reviewVo = reviewMap.get(userId);
				reviewVo.setNickName(userAccount.getName());
				reviewVo.setUserClass(userAccount.getUserLevel());
			}
		}
	}

	@Override
	public int getType() {
		return type;
	}

	@Override
	public int getStatus() {
		return status;
	}

	@Override
	public String getMsg() {
		return msg;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public void setProductType(int productType) {
		this.productType = productType;
	}

	public void setReviewType(int reviewType) {
		this.reviewType = reviewType;
	}

	public void setSkin(int skin) {
		this.skin = skin;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setReviewService(ReviewService reviewService) {
		this.reviewService = reviewService;
	}

	public List<ReviewVo> getReviews() {
		return reviews;
	}

}
